package com.globant.adapter;

import java.io.InputStream;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.globant.domain.Tweet;
import com.globant.twittersearch.ExternalStorage;
import com.globant.twittersearch.R;


public class TweetAdapter extends BaseAdapter {
	
	private List<Tweet> tweets;
	private ExternalStorage externalStorage;

	public TweetAdapter(List<Tweet> tweets) {
		this.tweets = tweets;	
		externalStorage = new ExternalStorage();
	}
	
	@Override
	public int getCount() {
		return tweets.size();
	}

	@Override
	public Object getItem(int position) {
		return tweets.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		TweetHolder holder = null;
		
		if(convertView == null) {
			item = new View(parent.getContext());
		} else {
			item = (View) convertView;
		}
		
        if (item == null || !(item.getTag() instanceof TweetHolder)) {
            
            LayoutInflater mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            item = mInflater.inflate(R.layout.tweet, parent, false);
            
            holder = new TweetHolder();
            populateView(holder, item, tweets.get(position));
            item.setTag(holder);
       }else{
           holder = (TweetHolder) item.getTag();
       }

		return item;
	}
	
	private void populateView(TweetHolder holder, View item, Tweet tweetBean) {
		
		holder.date = (TextView) item.findViewById(R.id.date);
		holder.userName = (TextView) item.findViewById(R.id.userName);
		holder.tweet = (TextView) item.findViewById(R.id.tweet);
		holder.icon = (ImageView) item.findViewById(R.id.icon);
		
		holder.tweet.setText(tweetBean.getText());
		holder.userName.setText(tweetBean.getFromUser());
		holder.date.setText(tweetBean.getFormattedDate());
		
		DownloadImageTask task = new DownloadImageTask(holder.icon);
		task.execute(tweetBean.getProfileImageUrl(), tweetBean.getFromUser());
	}
	

	class TweetHolder {
		public TextView date;
		public TextView userName;
		public TextView tweet;
		public ImageView icon;
	}
	
	class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
	    ImageView bmImage;

	    public DownloadImageTask(ImageView bmImage) {
	        this.bmImage = bmImage;
	    }

	    protected Bitmap doInBackground(String... param) {
	        Bitmap mIcon11 = null;
	        String userName = param[1];
	        
	        if(!externalStorage.existImage(userName)) {
	        	String urldisplay = param[0];
		        try {
		            InputStream in = new java.net.URL(urldisplay).openStream();
		            mIcon11 = BitmapFactory.decodeStream(in);
		            externalStorage.saveImage(mIcon11, userName);
		        } catch (Exception e) {
		            Log.e("Error", e.getMessage());
		        }
	        } else {
	        	mIcon11 = externalStorage.obtainImage(userName);
	        }
	        return mIcon11;
	    }

	    protected void onPostExecute(Bitmap result) {
	        bmImage.setImageBitmap(result);
	    }
	}
	
}
